package com.limetalks.util;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.limetalks.dao.StreamDAO;
import com.limetalks.dao.TestDAO;
import com.limetalks.dao.UserDAO;
import com.limetalks.data.Stream;
import com.limetalks.data.Test;
import com.limetalks.data.User;

public class TestMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		EntityManagerFactory emf = Persistence.createEntityManagerFactory("LimeTalksUnit");
		EntityManager em = emf.createEntityManager();
		
		TestDAO testDAO = new TestDAO(emf);
		Test test;
		
		Date d1;
		Date d2;
		Date d3;
		Date d4;
		Date d5;
		int length = 1;
		try{	
			d1 = new Date();
			int maxId = testDAO.getLastId();
//			List<Test> tests = testDAO.findAll();
//			em.getTransaction().begin();
			for(int i = 1; i <= length; i++){				
				d2 = new Date();
				test = new Test();
				test.setId(maxId + i);
				test.setField1("field1");
				test.setField2("field2");
				test.setField3("field3");
				
				testDAO.save(test);
//				em.persist(test);
				d3 = new Date();
				System.err.println("Test saved (" + (d3.getTime() - d2.getTime()) + " ms) - " + test);
				Integer lastId = testDAO.getLastId();
				d4 = new Date();
				System.err.println("Last id fetched (" + (d4.getTime() - d3.getTime()) + " ms) - " + lastId);
			}
//			em.getTransaction().commit();
			d5 = new Date();
			System.err.println(length + " Tests saved (" + (d5.getTime() - d1.getTime()) + " ms)");
//			System.err.println(tests.size() + " Tests found (" + (d4.getTime() - d1.getTime()) + " ms)");
		}finally{
			em.close();
			System.err.println("!!!!!! END");
		}
	}

}
